<template>
  <div class="transition_test">
    <button @click="show = !show">click</button>
    <transition name="img-fade">
      <img src="../../assets/img/logo.png" class="bigimg" alt="" v-if="show" />
    </transition>

    <div class="shop_cart">
      加入购物车动画(css实现):
      <div class="add">
        <button @click="addShopCart">add</button>
        <transition name="shop_cart">
          <img
            class="add_img"
            v-if="addShow"
            src="../../assets/img/logo.png"
            alt=""
          />
        </transition>
      </div>
      <div class="cart">
        <img src="../../assets/img/logo.png" alt="" />
      </div>
    </div>
    <!-- 动画数组效果 -->
    <ul class="group_list">
      <button @click="addItem">addItem</button>
      <button @click="removeItem">removeItem</button>
      <transition-group name="list">
        <li class="group_item" v-for="item in list" :key="item">{{ item }}</li>
      </transition-group>
    </ul>
    <button @click="joinShopping">加入购物车</button>
    <div class="ball-container" ref="ball">
      <span :style="ballStyle"></span>
    </div>
    <draggable
      v-model="myArray"
      group="people"
      @update="hanldeDragEnd"
      :move="getdata"
      @start="drag = true"
      @end="drag = false"
      item-key="id"
    >
      <template #item="{ element }">
        <div>
          <div>{{ element.name }}</div>
          <div class="dragli" v-for="item in fruits" :key="item.id">
            <span>{{ item.label }}</span>
          </div>
        </div>
      </template>
    </draggable>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, toRefs, onMounted } from "vue";
import draggable from "vuedraggable";
export default defineComponent({
  name: "transition",
  components: {
    draggable,
  },
  setup() {
    const show = ref(false);
    let timer: any = ref(null);
    let state = reactive({
      drag: false,
      myArray: [
        { id: 1, name: "Jenny" },
        { id: 2, name: "kevin" },
        { id: 3, name: "Rours" },
      ],
      fruits: [
        {
          id: 1,
          label: "苹果",
        },
        {
          id: 2,
          label: "香蕉",
        },
        {
          id: 3,
          label: "火龙果",
        },
      ],
    });
    let ballStyle = ref({});
    // let testReactive = reactive({ id: 1, name: "app" });
    const addShow = ref(true);
    let ball = ref<HTMLDivElement | null>(null);
    const list = reactive([1, 2, 3]);
    const addShopCart = () => {
      addShow.value = true;
      setTimeout(() => {
        addShow.value = false;
      }, 1000);
    };
    const addItem = () => {
      list.push(list.length + 1);
    };
    const removeItem = () => {
      list.length > 1 && list.pop();
    };
    const getdata = (val: any) => {
      console.log(val.draggedContext.element);
    };
    // 更新
    const hanldeDragEnd = (e: any) => {
      console.log(e);
      console.log("拖动前索引：" + e.oldIndex);
      console.log("拖动后索引：" + e.newIndex);
    };

    onMounted(() => {
      //为了防止火狐浏览器拖拽的时候以新标签打开，此代码真实有效
      document.body.ondrop = function (event) {
        event.preventDefault();
        event.stopPropagation();
      };
    });
    // function animationBall(callback: any) {
    //   let arrX: number[] = [],
    //     arrY: Array<number> = [], // 泛型
    //     i = 0;
    //   for (let x = 0; x > -151; x--) {
    //     let y = -(4 / 225) * x * x - (8 / 3) * x;
    //     arrX.push(x);
    //     arrY.push(y);
    //   }
    //   ball.value && (ball.value.style.display = "block");
    //   timer = window.setInterval(() => {
    //     i = i + 2;
    //     ballStyle.value = {
    //       width: 14 - (14 / 150) * i + "px",
    //       height: 14 - (14 / 150) * i + "px",
    //       transform: "translate(" + arrX[i] + "px,-" + arrY[i] + "px)",
    //       "-webkit-transform":
    //         "translate(" + arrX[i] + "px,-" + arrY[i] + "px)",
    //     };
    //     if (i >= 150) {
    //       window.clearInterval(Number(timer));
    //       ball.value && (ball.value.style.display = "none");
    //       ballStyle.value = {};
    //       callback();
    //     }
    //   }, 10);

    //   callback();
    // }
    const joinShopping = () => {
      // animationBall(() => {
      //   console.log("回调函数");
      // });
      // ball.value.style.display = 'block';
      // console.log(ballStyle, 999);
      // ballStyle.value = {
      //   width: '100px'
      // }
      // testReactive = {
      //   id: 2,
      //   name: 'RN'
      // }
      // console.log(ballStyle, testReactive);
    };
    return {
      show,
      ...toRefs(state),
      ballStyle,
      addShow,
      getdata,
      timer,
      addShopCart,
      list,
      ball,
      addItem,
      hanldeDragEnd,
      removeItem,
      joinShopping,
    };
  },
});
</script>

<style lang="scss" scoped>
.dragli {
  display: flex;
}
.ball-container {
  position: fixed;
  bottom: 100px;
  right: 30px;
  width: 160px;
  height: 110px;
  z-index: 999;
  display: none;
  span {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 14px;
    height: 14px;
    background-color: red;
    border-radius: 50%;
  }
}
.transition_test {
  text-align: left;
  padding-bottom: 300px;
  .bigimg {
    width: 100px;
    height: 100px;
    border-radius: 50%;
  }
  .img-fade-enter-active,
  .img-fade-leave-active {
    transition: all 0.5s ease-out;
  }
  .img-fade-enter,
  .img-fade-leave-to {
    // transform: translateX(500px);
    opacity: 0;
  }
  .shop_cart {
    padding: 20px;
  }
  .shop_cart .add {
    position: relative;
    margin-top: 100px;
    width: 500px;
    display: inline-block;
  }
  .shop_cart .add button {
    position: absolute;
    z-index: 99;
  }
  .shop_cart .cart {
    width: 300px;
    display: inline-block;
  }
  .shop_cart .cart img {
    width: 100%;
  }
  .shop_cart .add_img {
    width: 100px;
    position: absolute;
    left: 600px;
    top: -160px;
  }
  @keyframes bounce-in {
    0% {
      transform: scale(1);
      transform: translate(-600px, 100px);
    }
    100% {
      transform: scale(0.5);
      transform: translate(0px, 0px);
    }
  }
  .shop_cart-enter-active {
    animation: bounce-in 1s;
  }
  // 动态列表
  .group_list {
    padding: 30px;
  }
  .group_list .group_item {
    width: 200px;
    height: 100px;
    border: 1px solid orangered;
    margin: 10px;
  }
  .list-enter-active,
  .list-leave-active {
    transition: all 0.8s;
  }
  .list-enter,
  .list-leave-to {
    opacity: 0;
    transform: translateX(40px);
  }
}
</style>
